package com.zzyl.service.impl.handler;

import com.zzyl.entity.Resource;
import com.zzyl.vo.TreeItemVo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 过滤三级目录的链
 */
public class ThirdResourceFilterHandler extends ResourceFilterHandler{
    private ResourceFilterHandler nextHandler;

    public void setNextHandler(ResourceFilterHandler nextHandler) {
        this.nextHandler = nextHandler;
    }
    /**
     * @param allList          所有的菜单数据
     * @param root             根节点
     * @param parentResourceNo
     */
    @Override
    public void filter(List<Resource> allList, TreeItemVo root, String parentResourceNo) {
        root.getChildren().stream().map(r -> {
            //r 表示的就是一级目录
            List<TreeItemVo> secondList = r.getChildren(); // 获取二级目录
            secondList.stream().map(item -> {
                //item 表示的是二级菜单
               /* List<Resource> thridList = allList.stream().filter(i -> {
                    //i 表示的是菜单
                    if (i.getParentResourceNo().equals(item.getId())) {
                        return true;
                    } else {
                        return false;
                    }
                }).collect(Collectors.toList());*/
                List<Resource> thridList = listResource(allList, item.getId());

               /* List<TreeItemVo> thirdTreeItemVo = thridList.stream().map(m -> {
                    TreeItemVo v = new TreeItemVo();
                    v.setId(m.getResourceNo());
                    v.setLabel(m.getResourceName());
                    return v;
                }).collect(Collectors.toList());*/
                List<TreeItemVo> thirdTreeItemVo = parseItemVo(thridList);

                item.setChildren(thirdTreeItemVo);
                return item;
            }).collect(Collectors.toList());
            return r;
        }).collect(Collectors.toList());

        if(null != nextHandler){
            nextHandler.filter(allList, root, parentResourceNo);
        }
    }
}
